package org.jpab.tests;

import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import org.jpab.Callback;
import org.jpab.PortAudio;
import org.jpab.PortAudioException;
import org.jpab.Stream;
import org.jpab.StreamConfiguration;

public class OpenDefaultOutputStream {
	public static void main(String... arguments) throws Exception {
		PortAudio.initialize();
		final FileChannel channel = new FileInputStream("resources/test.raw").getChannel();
		Stream stream = PortAudio.createStream(PortAudio.getDefaultStreamConfiguration(StreamConfiguration.Mode.OUTPUT_ONLY), new Callback() {
			public State callback(ByteBuffer input, ByteBuffer output) {
				try {
					while (output.remaining() > 0)
						if(channel.read(output) == -1) 
							return State.COMPLETE;
					return State.RUNNING;
				} catch (IOException exception) {
					return State.ABORTED;
				}
			}
		}, new Runnable() {
			public void run() {
				try {
					PortAudio.terminate();
				} catch (PortAudioException ignore) { ignore.printStackTrace(); }
				try {
					channel.close();
				} catch (IOException ignore) { ignore.printStackTrace(); }
			}
		});
		stream.start();
		Thread.sleep(24000);
	}
}
